<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MyWebSocketEvent implements ShouldBroadcastNow// 不走队列
//class MyWebSocketEvent implements ShouldBroadcast//走队列，请先开启队列 php artisan queue:work --queue=your-queue-name--daemon

{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public string $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function broadcastOn()
    {
        // 返回一个私有通道
//        return [
//            new PrivateChannel('chat'),
//        ];

        // 返回一个公共通道
        return new Channel('chat');
    }

    public function broadcastAs()
    {
        return 'chat';
    }

    public function broadcastWith()
    {
        return ['event'=> 'chat','message' => $this->message, 'status' => 'ok']; // 定义客户端真正接收到的信息内容
    }
}
